package com.mrbeard.greenbroswer.acticity;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mrbeard.greenbroswer.R;
import com.mrbeard.greenbroswer.entites.Device;
import com.mrbeard.greenbroswer.utils.DeviceUtil;
import com.mrbeard.greenbroswer.utils.FileUtil;
import com.mrbeard.greenbroswer.utils.okhttputil.CallBackUtil;
import com.mrbeard.greenbroswer.utils.okhttputil.OkhttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class BindDeviceActivity extends AppCompatActivity {

    private static final int READ_PHONE_CODE = 1;
    private Button btn_BindDeviceByInput;
    private EditText et_bindDevice;

    private void initView() {
        btn_BindDeviceByInput = this.findViewById(R.id.btn_BindDeviceByInput);
        et_bindDevice = this.findViewById(R.id.et_bindDevice);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_device);
        initView();
    }

    private void sendMessageBindDevice() {
        //发送绑定请求
        String bindUrl = "http://www.scq725.cn:8080/greenbrowser/user/add_device";
        DeviceUtil deviceUtil = new DeviceUtil(this);
        Device device = deviceUtil.getDevice();
        Map map = new HashMap();
        map.put("userId", et_bindDevice.getText().toString());
        map.put("device", device);
        Gson gson = new Gson();
        OkhttpUtil.okHttpPostJson(bindUrl, gson.toJson(map), new CallBackUtil() {
            @Override
            public Object onParseResponse(Call call, Response response) {
                return null;
            }

            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(getApplicationContext(), "绑定失败，请联系管理员", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(Object response) {
                Toast.makeText(getApplicationContext(), "绑定成功！", Toast.LENGTH_LONG).show();
            }
        });
    }
    public void bindDeviceByInput(View view) {

        if (et_bindDevice.getText().toString().equals("")) {
            AlertDialog dialog = new AlertDialog.Builder(this)
                    //.setIcon(R.mipmap.icon)//设置标题的图片
                    //.setTitle("  ")//设置对话框的标题
                    .setMessage("请输入绑定码")//设置对话框的内容
                    //设置对话框的按钮
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create();
            dialog.show();
        } else {
            if (Build.VERSION.SDK_INT > 22) {
                if (ContextCompat.checkSelfPermission(BindDeviceActivity.this,
                        Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    //先判断有没有权限 ，没有就在这里进行权限的申请
                    ActivityCompat.requestPermissions(BindDeviceActivity.this,
                            new String[]{android.Manifest.permission.READ_PHONE_STATE}, READ_PHONE_CODE);

                } else {
                    //说明已经获取到权限了 想干嘛干嘛
                   sendMessageBindDevice();
                }
            } else {
                //这个说明系统版本在6.0之下，不需要动态获取权限。
               sendMessageBindDevice();
            }
        }

    }

    //获取权限回调函数
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {

            case READ_PHONE_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //这里获取读取手机信息权限
                    sendMessageBindDevice();

                } else {
                    //这里是拒绝给APP获取手机信息权限，给个提示什么的说明一下都可以。
                    Toast.makeText(BindDeviceActivity.this, "请手动打开获取手机信息权限", Toast.LENGTH_SHORT).show();
                }
                break;

            default:
                break;
        }

    }
}
